// pages/registerPersonal/registerPersonal.js
// 导入dialog
// 统一资源信息管理
const { GetVote } = require("../../services/voteCommitReq");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    voteList: [],
    powerShow: false,
    pageSize: 10,
    pageNum: 1,
    isFinish: false,
  },
  onReachBottom() {
    if (isFinish) {
      return wx.showToast({
        title: "到底了……",
        icon: "error",
      });
    }

    this.setData({
      pageNum: this.data.pageNum + 1,
    });
    return this.handleGetVote();
  },

  onLoad: function (options) {
    this.handleGetVote();
    let volunteerInfo = wx.getStorageSync("volunteerInfo").volunteerId;
    if (!volunteerInfo) {
      wx.showToast({
        title: "请先登陆",
        icon: "error",
        duration: 1000,
        success(res) {
          setTimeout(
            () =>
              wx.navigateTo({
                url: "../login/login",
              }),
            1000
          );
        },
      });
    }
  },

  handleGetVote() {
    const { pageSize, pageNum, voteList } = this.data;
    const params = {
      pageSize,
      pageNum,
    };
    wx.showLoading({
      title: "加载中",
      mask: true,
    });

    GetVote(params, (res) => {
      console.log(res.data.data);
      const { totalCount, list } = res.data.data;
      this.setData({
        voteList: voteList.concat(list),
        isFinish: voteList.length >= totalCount,
      });

      wx.hideLoading(res);
    });
  },
  handleGoDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../voteDetail/voteDetail?id=" + id,
    });
  },
  // 授权信息
  handleGetPower(e) {
    if (e.detail.userInfo) {
      wx.showToast({
        title: "授权成功",
      });
      this.setData({
        powerShow: false,
      });

      // 获取用户信息
      wx.userProfile({
        lang: "zh_CN",
        success: (res) => {
          wx.hideLoading();
        },
        fail: (res) => {
          this.setData({
            powerShow: true,
          });
        },
      });
    } else {
      wx.showToast({
        title: "授权失败",
        icon: "none",
      });
    }
  },
});
